package com.example.ujsfish.coord;

import android.util.Log;

import static java.lang.Math.PI;

//将经纬度转换为平面坐标
public class CoordinateXY {
    static final double earthR = 63713930;//分米
    private static final String TAG = "CoordinateXY";
    private double movetoplanex; //设定目标点的平面坐标

    private double movetoplaney;
    private String movetoplaneENx;//平面坐标转换成东南西北坐标 X东正西负 Y北正南负
    private String movetoplaneENy;
    private double lng0; //码头原点经纬度坐标
    private double lat0;

    @Override
    public String toString() {
        return "CoordinateXY{" +
                "movetoplaneENx='" + movetoplaneENx + '\'' +
                ", movetoplaneENy='" + movetoplaneENy + '\'' +
                '}';
    }

    public String getMovetoplaneENx() {
        return movetoplaneENx;
    }

    public String getMovetoplaneENy() {
        return movetoplaneENy;
    }

    public CoordinateXY(String lng0, String lat0) {

        double lng00 = Double.parseDouble(lng0.substring(0, lng0.length() - 1));
        double lat00 = Double.parseDouble(lat0.substring(0, lat0.length() - 1));
        System.out.println("去掉字母    " + lng00 + "------" + lat00);
        this.lng0 = changePoint(lng00);
        this.lat0 = changePoint(lat00);
        System.out.println("转换    " + this.lng0 + "------" +  this.lat0);

    }

    /*
                Dim data As Double
                data = Val(GNGGAdata)
                data = Int(data / 100) + (data - Int(data / 100) * 100) / 60
                GNGGAtoDegree = Round(data, 8)
     */

    private double changePoint( double point) {
//        int round = (int) Math.floor(point / 100);
//        Double result = round + (point - round*100)/60;
//        Double result1 = (double)(Math.round(result*100000000))/100000000;
//        System.out.println(result1);
//        return result1;


        double result = (int)(point / 100) + (point - (int)(point / 100) * 100) / 60;
        result = Math.round(result *100000000);
        result = result / 100000000;
        System.out.println("转变" + result);
        return result;
    }

    public void getCoordinate(double lng, double lat) {

//        lng = changePoint(lng);
//        lat = changePoint(lat);
//        lng = changePoint(lng);
//        lat = changePoint(lat);
        System.out.println("lng =" + lng);
        System.out.println("lat =" + lat);
        /*
        movetoplanex = Math.cos(lat * PI / 180) * (lng - lng0) * earthR * PI / 180 + 10000;
        //0.01745329251994329444 =>pi/180, 1112017.857881906662027222=>R*pi/180
        movetoplaney = (lat - lat0) * earthR * PI / 180 + 10000; //R=63713930dm, pi=3.1415926535,
         */

        movetoplanex = Math.cos(lat * PI / 180) * (lng - lng0) * earthR * PI / 180 + 10000;
        movetoplaney = (lat - lat0) * earthR * PI / 180 + 10000; //R=63713930dm, pi=3.1415926535,
        System.out.println("movetoplanex =" + movetoplanex);
        System.out.println("movetoplaney =" + movetoplaney);


        double aa;
        double bb;
        if (movetoplanex > 10000) {
            aa = (movetoplanex - 10000) / 10;
            //aa = aa.toFixed(1);
            bb = (Math.round(aa * 10));
            aa = bb/10;
            System.out.println("aa ===========" + aa);
            if (aa < 10) {
                movetoplaneENx = new String("E00" + aa);
            } else if (aa >= 10 && aa < 100) {
                movetoplaneENx = "E0" + aa;
            } else if (aa >= 100) {
                movetoplaneENx = "E" + aa;
            }
        } else {
            aa = (10000 - movetoplanex) / 10;
            //aa = aa.toFixed(1);
            bb = (Math.round(aa * 10));
            aa = bb/10;
            System.out.println("aa ===========" + aa);
            if (aa < 10) {
                movetoplaneENx = "W00" + aa;
            } else if (aa >= 10 && aa < 100) {
                movetoplaneENx = "W0" + aa;
            } else if (aa >= 100) {
                movetoplaneENx = "W" + aa;
            }
        }
        if (movetoplaney > 10000) {
            aa = (movetoplaney - 10000) / 10;
            //aa = aa.toFixed(1);
            bb = (Math.round(aa * 10));
            aa = bb/10;
            System.out.println("aa ===========" + aa);
            if (aa < 10) {
                movetoplaneENy = "N00" + aa;
            } else if (aa >= 10 && aa < 100) {
                movetoplaneENy = "N0" + aa;
            } else if (aa >= 100) {
                movetoplaneENy = "N" + aa;
            }
        } else {
            aa = (10000 - movetoplaney) / 10;
            //aa = aa.toFixed(1);
            bb = (Math.round(aa * 10));
            aa = bb/10;
            System.out.println("aa ===========" + aa);
            if (aa < 10) {
                movetoplaneENy = "S00" + aa;
            } else if (aa >= 10 && aa < 100) {
                movetoplaneENy = "S0" + aa;
            } else if (aa >= 100) {
                movetoplaneENy = "S" + aa;
            }
        }
        Log.d(TAG, "getCoordinate: " + movetoplaneENx + "......" + movetoplaneENy);

    }



}
